package com.lonely.fx.table_view;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * @date: 2022-08-02 15:55
 * @author: ztkj-hzb
 * @description: tableview 基于 map类型处理
 **/
public class TableViewWithMapSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        ObservableList<Map<String, String>> list = FXCollections.observableArrayList();
        HashMap<String, String> hashMap1 = new HashMap<>();
        hashMap1.put("name", "aa");
        hashMap1.put("age", "20");
        hashMap1.put("address", "湖北省");
        HashMap<String, String> hashMap2 = new HashMap<>();
        hashMap2.put("name", "bb");
        hashMap2.put("age", "25");
        hashMap2.put("address", "湖南省");
        HashMap<String, String> hashMap3 = new HashMap<>();
        hashMap3.put("name", "cc");
        hashMap3.put("age", "30");
        hashMap3.put("address", "浙江省");

        list.addAll(hashMap1, hashMap2, hashMap3);

        TableView<Map<String, String>> tableView = new TableView<>(list);

        tableView.setPrefSize(400, 400);

        TableColumn<Map<String, String>, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<Map<String, String>, String> ageTableCol = new TableColumn<>("年龄");
        TableColumn<Map<String, String>, String> addressTableCol = new TableColumn<>("地址");

        //绑定数据
        nameTableCol.setCellValueFactory(new MapValueFactory("name"));
        ageTableCol.setCellValueFactory(new MapValueFactory("age"));
        addressTableCol.setCellValueFactory(new MapValueFactory("address"));

        tableView.getColumns().addAll(nameTableCol, ageTableCol, addressTableCol);
        anchorPane.getChildren().add(tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


    }
}
